import {
  AnswerItem,
  ConclusionQuestionItem,
  GroupFeedbackItem,
  GroupAnswerItem
} from './group-score.d';
import {
  ASYMPTOMATIC,
  SYMPTOMATIC,
  OBVIOUS_SYMPTOMS
} from '../../../constants/result';
import {
  SYMPTOM_START,
  SYMPTOM_OBVIOUS_START
} from '../../../constants/feedback';

class GroupScore {
  /** 总成绩 */
  totalScore: number;

  /** 总反馈信息 */
  description: string;

  /** 当前用户的成绩列表 */
  scoreList: number[];

  /** 当前用户的回答（经过conclusion分组） */
  currentAnswerList: GroupAnswerItem[];

  /** 通过conclusion对问题进行分类 */
  static conclusionList: ConclusionQuestionItem[];

  static initConclusionList(conclusionList: ConclusionQuestionItem[]) {
    this.conclusionList = conclusionList;
  }

  constructor(
    currentAnswerList: AnswerItem[],
    colusionFeedbackList?: GroupFeedbackItem[]
  ) {
    this.setScoreList(currentAnswerList);
    this.setTotalScore();
    this.setDescription();
    this.setCurrentUserAnswer(currentAnswerList);
    this.sortConclusionById();
    if (colusionFeedbackList) {
      this.setUserAnswerWithDescription(colusionFeedbackList);
    }
  }

  public setScoreList(currentAnswerList: AnswerItem[]) {
    const answerList = currentAnswerList.map((answerItem: AnswerItem) => answerItem.score);
    this.scoreList = answerList;
  }

  private setTotalScore() {
    this.totalScore = this.scoreList.reduce((prev, curv) => prev + curv, 0);
  }

  private setDescription() {
    if (this.totalScore < SYMPTOM_START) {
      this.description = ASYMPTOMATIC;
      return;
    }
    if (this.totalScore < SYMPTOM_OBVIOUS_START) {
      this.description = SYMPTOMATIC;
      return;
    }

    this.description = OBVIOUS_SYMPTOMS;
  }

  private setCurrentUserAnswer(currentAnswerList: AnswerItem[]) {
    this.currentAnswerList = GroupScore.conclusionList.map((conclusionItem) => {
      const { conclusionId, conclusionName, questionList } = conclusionItem;
      let totalScore = 0;

      questionList.forEach((currentQuestionId: number) => {
        const curAnswerItem = currentAnswerList.find((
          answerItem: AnswerItem
        ) => answerItem.questionId === currentQuestionId);

        if (!curAnswerItem) {
          // hack 的错误处理, 未知原因问卷答案缺失, 但认为答完问卷
          totalScore += 1;
          console.error('并未查询到用户回答了该分类下的问题');
        } else {
          totalScore += curAnswerItem.score;
        }

        return totalScore;
      });
      return {
        conclusionId,
        conclusionName,
        totalScore
      };
    });
  }

  private setUserAnswerWithDescription(
    colusionFeedbackList: GroupFeedbackItem[]
  ) {
    const conclusionAnswerListWithoutDes = this.currentAnswerList;
    const conclusionAnswerListWithDes = conclusionAnswerListWithoutDes.map((item) => {
      const { conclusionId, conclusionName, totalScore } = item;

      const conclusionFeedback = colusionFeedbackList.find((
        currentConclusionFeedback
      ) => {
        const {
          conclusionId: itemConclusionId,
        } = currentConclusionFeedback;
        return itemConclusionId === conclusionId;
      });

      if (!conclusionFeedback) {
        // 在feedback中找不到当前conclusionId对应的记录
        throw new Error('反馈信息错误！');
      }

      const feedbackItem = conclusionFeedback.feedbacks.find((element) => {
        const { upper, lower } = element;
        return totalScore >= lower && totalScore <= upper;
      });

      return {
        conclusionId,
        conclusionName,
        totalScore,
        description: feedbackItem?.description
      };
    });
    this.currentAnswerList = conclusionAnswerListWithDes;
  }

  private sortConclusionById() {
    this.currentAnswerList.sort((a, b) => (a.conclusionId - b.conclusionId));
  }
}

export default GroupScore;
